﻿using ZYCommon;
using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;

namespace TextEditor
{
    

    public class ZYVBConnection
    {
        private ZYDBConnection myDBConn = null;
        private Hashtable myListTable = new Hashtable();

        public ZYVBConnection(ZYDBConnection conn)
        {
            this.myDBConn = conn;            
        }

        public bool BuildListItem(string strName, string strSQL)
        {
            try
            {
                using (IDbCommand command = this.myDBConn.CreateCommand())
                {
                    ArrayList list = new ArrayList();
                    command.CommandText = strSQL;
                    IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);
                    if (reader.FieldCount >= 2)
                    {
                        while (reader.Read())
                        {
                            DBListItem item = new DBListItem();
                            item.ItemName = reader.IsDBNull(0) ? null : reader[0].ToString();
                            item.ItemValue = reader.IsDBNull(1) ? null : reader[1].ToString();
                            list.Add(item);
                        }
                    }
                    reader.Close();
                    this.myListTable.Add(strName, list);
                    return true;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("加载列表错误" + exception.ToString());
            }
            return false;
        }

        public string[] GetFirstFieldValues(string strSQL)
        {
            return this.myDBConn.GetValueList(strSQL, true, false);
        }

        public string[] GetFirstRecordValue(string strSQL)
        {
            return this.myDBConn.GetValueList(strSQL, false, true);
        }

        public string GetListItemValue(string strName, string strCode)
        {
            ArrayList list = this.myListTable[strName] as ArrayList;
            if (list != null)
            {
                foreach (DBListItem item in list)
                {
                    if (item.ItemName == strName)
                    {
                        return item.ItemValue;
                    }
                }
            }
            return null;
        }

        public string GetSingleValue(string strSQL)
        {
            return this.myDBConn.GetSingleValue(strSQL);
        }

        private class DBListItem
        {
            public string ItemName;
            public string ItemValue;
        }
    }
}

